home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFcolours / c / Main < prev    next >
Encoding:
Text File  |  2003-11-06  |  9.4 KB  |  321 lines

  1. /*
  2.  *  SFcolours - Star Fighter 3000 colours editor
  3.  *  Main application skeleton
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdbool.h>
  25. #include <signal.h>
  26.  
  27. /* RISC OS library files */
  28. #include "wimp.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "wimplib.h"
  32. #include "window.h"
  33.  
  34. /* My library files */
  35. #include "err.h"
  36. #include "msgtrans.h"
  37. #include "hourglass.h"
  38. #include "Macros.h"
  39. #include "Loader.h"
  40. #include "Pal256.h"
  41. #include "ViewsMenu.h"
  42. #include "InputFocus.h"
  43.  
  44. /* Local headers */
  45. #include "SFCIconbar.h"
  46. #include "SFCSaveBox.h"
  47. #include "SFCFileInfo.h"
  48. #include "Menus.h"
  49. #include "SFCCreateFile.h"
  50. #include "PreQuit.h"
  51. #include "DCS_dialogue.h"
  52. #include "Main.h"
  53. #include "Utils.h"
  54.  
  55. #define WimpVersion    310
  56.  
  57. /* Our toolbox events */
  58. #define EVENT_HELPFILE    0x03
  59. #define EVENT_QUIT        0x09
  60.  
  61. _kernel_oserror shared_err_block = {255, ""};
  62. int palette[256];
  63. ObjectId pal256_sharedid = NULL_ObjectId;
  64. char taskname[32];
  65. #ifdef INT_COLOUR_FIND
  66. bool             use_colour_trans = false;
  67. #endif
  68.  
  69. static  WimpPollBlock poll_block;
  70. static  IdBlock       id_block;
  71.  
  72. /* ----------------------------------------------------------------------- */
  73. /*                       Function prototypes                               */
  74.  
  75. static ToolboxEventHandler tb_quithandler, showhelp_handler, autocreate_handler, error_handler;
  76. static WimpMessageHandler PreQuit_handler, wimpquit_handler;
  77. static void initialise(void);
  78. #ifdef INT_COLOUR_FIND
  79. static void process_arguments(int argc, char *argv[]);
  80. #endif
  81. static void simple_exit(_kernel_oserror *e);
  82.  
  83. /* ----------------------------------------------------------------------- */
  84. /*                         Public functions                                */
  85.  
  86. int main(int argc, char *argv[])
  87. {
  88.   /* CLI parameters are ignored, as we claim no filetypes */
  89.   int event_code;
  90.  
  91.   hourglass_on();
  92. #ifdef INT_COLOUR_FIND
  93.   process_arguments(argc, argv);
  94. #endif
  95.   initialise();
  96.   hourglass_off();
  97.  
  98.   /*
  99.    * poll loop
  100.    */
  101.   while (TRUE)
  102.     event_poll (&event_code, &poll_block, 0);
  103. }
  104.  
  105. /* ----------------------------------------------------------------------- */
  106. /*                         Private functions                               */
  107.  
  108. static int tb_quithandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  109. {
  110.   /* Quit entry on iconbar menu */
  111.   if(TRY_QUIT(0))
  112.     exit(EXIT_SUCCESS);
  113.  
  114.   return 1; /* claim event */
  115. }
  116.  
  117. /* ----------------------------------------------------------------------- */
  118.  
  119. static int PreQuit_handler(WimpMessage *message, void *handle)
  120. {
  121.   int th;
  122.   if(message->hdr.size < 20 || message->data.words[0] == 0)
  123.     th = message->hdr.sender; /* shutdown in progress */
  124.   else
  125.     th = 0; /* just our task */
  126.  
  127.   /* If function returns false then unsaved data */
  128.   if(!TRY_QUIT(th)) {
  129.  
  130.     /* Object by acknowledging message */
  131.     message->hdr.your_ref = message->hdr.my_ref;
  132.     RE(wimp_send_message(Wimp_EUserMessageAcknowledge, message, message->hdr.sender, NULL, NULL))
  133.   }
  134.   return 1; /* claim event */
  135. }
  136.  
  137. /* ----------------------------------------------------------------------- */
  138.  
  139. static int wimpquit_handler(WimpMessage *message,void *handle)
  140. {
  141.   /* Quit application immediately */
  142.   exit(EXIT_SUCCESS);
  143.   
  144.   return 1; /* claim event */  
  145. }
  146.  
  147. /* ----------------------------------------------------------------------- */
  148.  
  149. static int showhelp_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  150. {
  151.   /* Show application help file */
  152.   if(_kernel_oscli("Filer_Run <SFcolours$Dir>.!Help") == _kernel_ERROR)
  153.     err_check_rep(_kernel_last_oserror());
  154.     
  155.   return 1; /* claim event */
  156. }
  157.  
  158. /* ----------------------------------------------------------------------- */
  159.  
  160. static int autocreate_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  161. {
  162.   /* Catch auto-created objects and initialise handlers etc. */
  163.   ToolboxObjectAutoCreatedEvent *toace = (ToolboxObjectAutoCreatedEvent *)event;
  164.   if (strcmp(toace->template_name, "Iconbar") == 0) {
  165.     Iconbar_initialise(id_block->self_id);
  166.     return 1; /* claim event */
  167.   }
  168.   if (strcmp(toace->template_name, "CreateFile") == 0) {
  169.     CreateFile_initialise(id_block->self_id);
  170.     return 1; /* claim event */
  171.   }
  172.   if (strcmp(toace->template_name, "Menu") == 0) {
  173.     EF(ViewsMenu_parentcreated(id_block->self_id, 0x03));
  174.     return 1; /* claim event */
  175.   }
  176.   if (strcmp(toace->template_name, "PreQuit") == 0) {
  177.     PreQuit_initialise(id_block->self_id);
  178.     return 1; /* claim event */
  179.   }
  180.   if (strcmp(toace->template_name, "ColsMenu") == 0) {
  181.     ColsMenu_initialise(id_block->self_id);
  182.     return 1; /* claim event */
  183.   }
  184.   if (strcmp(toace->template_name, "FileMenu") == 0) {
  185.     FileMenu_initialise(id_block->self_id);
  186.     return 1; /* claim event */
  187.   }
  188.   if (strcmp(toace->template_name, "EditMenu") == 0) {
  189.     EditMenu_initialise(id_block->self_id);
  190.     return 1; /* claim event */
  191.   }
  192.   if (strcmp(toace->template_name, "SaveFile") == 0) {
  193.     SaveFile_initialise(id_block);
  194.     return 1; /* claim event */
  195.   }
  196.   if (strcmp(toace->template_name, "FileInfo") == 0) {
  197.     FileInfo_initialise(id_block);
  198.     return 1; /* claim event */
  199.   }
  200.   if (strcmp(toace->template_name, "Pal256") == 0) {
  201.     EF(Pal256_initialise(id_block->self_id));
  202.     EF(event_register_toolbox_handler(id_block->self_id, Window_AboutToBeShown, InputFocus_recordcaretpos, NULL));
  203.     pal256_sharedid = id_block->self_id; /* we only have one palette */
  204.     return 1; /* claim event */
  205.   }
  206.   if (strcmp(toace->template_name, "DCS") == 0) {
  207.     DCS_initialise(id_block->self_id);
  208.     return 1; /* claim event */
  209.   }
  210.  
  211.   return 0; /* event not handled */
  212. }
  213.  
  214. /* ----------------------------------------------------------------------- */
  215.  
  216. static int error_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  217. {
  218.   ToolboxErrorEvent *totee = (ToolboxErrorEvent *)event;
  219.   if(totee->errnum == 0x80b633 || totee->errnum == 0x131c3) /* "To save drag...", locked file */
  220.     err_report(totee->errnum, totee->errmess);
  221.   else
  222.     err_complain(totee->errnum, totee->errmess);
  223.  
  224.   return 1; /* claim event */
  225. }
  226.  
  227. /* ----------------------------------------------------------------------- */
  228.  
  229. static void initialise(void)
  230. {
  231.   int    toolbox_events = 0,
  232.          wimp_messages = 0,
  233.          wimpversion;
  234.  
  235.   /*
  236.    * Prevent termination on SIGINT (we use the escape key ourselves)
  237.    */
  238.    signal(SIGINT, SIG_IGN);
  239.  
  240.   /*
  241.    * register ourselves with the Toolbox.
  242.    */
  243.  
  244.   {
  245.     _kernel_oserror *e = toolbox_initialise (0, WimpVersion, &wimp_messages, &toolbox_events, "<SFcoloursRes$Dir>",msgs_get_descriptor(), &id_block, &wimpversion, 0, 0);
  246.     if(e != NULL)
  247.       simple_exit(e);
  248.   }
  249.  
  250.   strncpy(taskname, msgs_lookup("_TaskName"), sizeof(taskname)-1);
  251.   err_set_taskname(taskname, (wimpversion >= 321));
  252.  
  253.   /*
  254.    * initialise the flex library
  255.    */
  256.  
  257.   flex_init(taskname, (int *)msgs_get_descriptor(), 0); /* (use Wimpslot and own messages file) */
  258.   flex_set_budge(1); /* allow budging of flex when heap extends */
  259.  
  260.   /*
  261.    * initialise the event library.
  262.    */
  263.  
  264.   event_initialise (&id_block);
  265.   event_set_mask (Wimp_Poll_NullMask |
  266.                   Wimp_Poll_KeyPressedMask);
  267.  
  268.   EF(event_register_toolbox_handler(-1, Toolbox_ObjectAutoCreated, autocreate_handler, 0));
  269.   EF(event_register_toolbox_handler(-1, Toolbox_Error, error_handler, 0));
  270.   EF(event_register_toolbox_handler(-1, EVENT_HELPFILE, showhelp_handler, 0));
  271.   EF(event_register_toolbox_handler(-1, EVENT_QUIT, tb_quithandler, 0));
  272.   EF(event_register_message_handler(Wimp_MPreQuit, PreQuit_handler, 0));
  273.   EF(event_register_message_handler(Wimp_MQuit, wimpquit_handler, 0));
  274.   EF(InputFocus_initialise());
  275.  
  276.   /* Initialise Loader */
  277.   EF(loader_initialise(0));
  278.  
  279.   /* Read the default mode 13 palette */
  280.   {
  281.     _kernel_swi_regs regs;
  282.     regs.r[0] = 13; /* mode 13 */
  283.     regs.r[1] = 0; /* read default palette */
  284.     regs.r[2] = (int)palette; /* pointer to buffer */
  285.     regs.r[3] = sizeof(palette); /* size of buffer */
  286.     regs.r[4] = 0; /* flags */
  287.     EF(_kernel_swi(ColourTrans_ReadPalette, ®s, ®s));
  288.   }
  289.   
  290.   EF(ViewsMenu_create());
  291. }
  292.  
  293. /* ----------------------------------------------------------------------- */
  294.  
  295. static void simple_exit(_kernel_oserror *e)
  296. {
  297.   /* Limited amount we can do with no messages file... */
  298.   wimp_report_error(e, Wimp_ReportError_Cancel, "SFcolours");
  299.   exit(EXIT_FAILURE);
  300. }
  301.  
  302. /* ----------------------------------------------------------------------- */
  303.  
  304. #ifdef INT_COLOUR_FIND
  305. static void process_arguments(int argc, char *argv[])
  306. {
  307.   /*
  308.    * Look at command-line parameters
  309.    */
  310.  
  311.   for(int i = 1; i < argc; i++) {
  312.     if(ViewsMenu_strcmp_nc(argv[i], "-usect"))
  313.       use_colour_trans = true;
  314.     else {
  315.       strcpy(shared_err_block.errmess, "Bad command line parameters");
  316.       simple_exit(&shared_err_block);
  317.     }
  318.   }
  319. }
  320. #endif
  321.